package com.ruis.kotboot.common.plug;

import javax.servlet.http.HttpServletResponse;
import java.io.*;

public class FileUtils {

    public static void writeImage(File filePic,HttpServletResponse response) throws IOException {
       // File filePic = new File(path+"1-ab1.png");
        if(filePic.exists()){
            FileInputStream is = new FileInputStream(filePic);
            int i = is.available(); // 得到文件大小
            byte data[] = new byte[i];
            is.read(data); // 读数据
            is.close();
            response.setContentType("image/*"); // 设置返回的文件类型
            OutputStream toClient = response.getOutputStream(); // 得到向客户端输出二进制数据的对象
            toClient.write(data); // 输出数据
            toClient.close();
        }
    }
    public static void writeImage(String path, HttpServletResponse response) throws IOException {
        try{
            File file = new File(path);
            if (file.exists()) {
                DataOutputStream temps = new DataOutputStream(response.getOutputStream());
                response.setContentType("image/*");
                response.setHeader("Content-Type","image/*");
                DataInputStream in = new DataInputStream(
                        new FileInputStream(path));
                byte[] b = new byte[2048];
                while ((in.read(b)) != -1) {
                    temps.write(b);
                    temps.flush();
                }
                in.close();
                temps.close();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
